package day23;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class PrintNumberCharOfLock {
	public static void main(String[] args) {
		Lock lock = new ReentrantLock();
		Condition c = lock.newCondition();
		Thread t1 = new NumberThread1(lock, c);
		Thread t2 = new CharThread1(lock, c);
		t1.start();
		t2.start();
	}
}

class NumberThread1 extends Thread {
	private Lock lock;
	private Condition c;

	public NumberThread1(Lock lock, Condition c) {
		this.lock = lock;
		this.c = c;
	}

	public void run() {
		lock.lock();
		try {
			for (int i = 1; i <= 26; i++) {
				System.out.println(i * 2 - 1);
				System.out.println(i * 2);
				c.signalAll();
				if (i != 26) {
					try {
						c.await();
					} catch (Exception e) {
					}
				}
			}
		} finally {
			lock.unlock();
		}

	}
}

class CharThread1 extends Thread {
	private Lock lock;
	private Condition cd;

	public CharThread1(Lock lock, Condition c) {
		this.lock = lock;
		this.cd = c;
	}

	public void run() {
		lock.lock();
		try {
			for (char c = 'A'; c <= 'Z'; c++) {
				System.out.println(c);
				cd.signalAll();
				if (c != 'Z') {
					try {
						cd.await();
					} catch (Exception e) {
					}
				}
			}
		} finally {
			lock.unlock();
		}

	}
}
